﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：Cart.cs
//
//
//文件功能描述：显示一个类别的具体的商品..
//
//
//创建标识：2007.11.21 王志平
//创建描述：
//
//修改描述:2007.11.30 王志平
 //修改标识:重新修改了翻页方法.
//
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using localhost;

public partial class UserControl_KindsProducts : System.Web.UI.UserControl
{
    private static PagedDataSource pds;
    //
    //纪录的总条数
    //
    private static int iTotalNum;
    //
    //总的页数
    //
    private static int iPageCount; 
    //
    //总的记录数
    //
    private static int iRecords;  
    //
    //调用WebServer的方法..
    //
    localhost.ProductService myOperate = new localhost.ProductService();
    //
    //初始化strUpperID;
    //
    static string strUpperID=string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {  
        //
        //第一次启动页面执行的代码
        //
        if (!IsPostBack)
        {
            //
            //依据查询字符串得到大类别的ID值
            //
            strUpperID = Request.QueryString["upperID"].ToString();
            //
            //显示当前的位置.
            //
            DisplayLocation();
            //
            //绑定第一页时.
            //
            BindDataList(1);
            //
            //获取页的总数
            //
            GetProductCount();
            //
            //绑定页的选择索引
            //
            BindDropdownListPageIndex();
        }
    }
    /// <summary>
    /// 显示当前页面的位置
    /// </summary>
    protected void DisplayLocation()
    {
       

       if (strUpperID != null||strUpperID!="")
       {
           //
           //实例化strUpperID对象.
           //
           localhost.UpperCategory upperCategory = new localhost.UpperCategory();
           //
           //调用大类别的实例
           //
           upperCategory = myOperate.GetUpperCategoryModel(strUpperID);
           //
           //显示当前的位置
           //
           Location1.strLabelText = Location1.strLabelText + ">" + upperCategory.Name;

           Page.Title = "商品分类---" + upperCategory.Name;
           
       }
    }
    /// <summary>
    /// 数据绑定
    /// </summary>
    /// <param name="iPageIndex">当前页面的索引</param>
    protected void BindDataList(int iPageIndex)
    {
        //
        //页面大小
        //
        int iPageSize = 8;
        //
        //对WebServer的异步调用.
        //
        myOperate.GetProductListByCategoryIDCompleted += new GetProductListByCategoryIDCompletedEventHandler(myOperate_GetProductListByCategoryIDCompleted);
        myOperate.GetProductListByCategoryIDAsync(iPageSize, iPageIndex, strUpperID);
        //
        //显示当前页
        //
        lblPage.Text = iPageIndex.ToString();

        if (iPageIndex == 1)
        {
            //
            //如果当前页为第一页
            //
            ddlPage.SelectedValue = "0";
        }//if (iPageIndex == 1)
        else
        {
            ddlPage.SelectedValue = (iPageIndex - 1).ToString();
        }

        //
        //显示分页按钮的可用性
        //
        ShowButtonPaging();

    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender">对象</param>
    /// <param name="e">对象参数</param>
    void myOperate_GetProductListByCategoryIDCompleted(object sender, GetProductListByCategoryIDCompletedEventArgs e)
    {
     
        DataSet dsProductList = new DataSet();
        //
        //对异步调用返回的值进行判断.
        //
        if (e.Result != null)
        {   
            //
            //如果返回结果不空
            //
            dsProductList = e.Result;
            //
            //DataView视图作数据源
            //
            DataView dvProductList = dsProductList.Tables[0].DefaultView;
            //
            //对视图排序,根据下拉列表的选择
            //
            dvProductList.Sort = dropSort.SelectedValue + " ASC";
            //
            //绑定数据源
            //
            dlstProduct.DataSource = dvProductList;
            dlstProduct.DataBind();
        }
    }
    /// <summary>
    /// 获取总的记录条数
    /// </summary>
    protected void GetProductCount()
    {

        //
        //返回记录的总数.
        //
        iRecords = myOperate.GetProductCountByCategoryID(strUpperID);
        //
        //求出页面的页数.
        //
        if (iRecords % 8 == 0)
        {
            //
            //记录数刚好可以整除页大小
            //
            iPageCount = iRecords / 8;
        }
        else
        {
            //
            //记录数不能整除页大小
            //
            iPageCount = Convert.ToInt32(iRecords / 8) + 1;
        }

        lblRecords.Text = iRecords.ToString();
    }
    /// <summary>
    /// 绑定用来选择页导航的下拉列表
    /// </summary>
    protected void BindDropdownListPageIndex()
    {
        //
        //设定一个ListItem对象.
        //
        ListItem lt;
        for (int i = 0; i < iPageCount; i++)
        {
            lt = new ListItem();
            //
            //绑定其Text值和value值
            //
            lt.Text = Convert.ToString(i + 1);
            lt.Value = i.ToString();
            //
            //将值添加到ddlPage中.
            //
            ddlPage.Items.Add(lt);
        }
    }

    /// <summary>
    /// 设定分页按钮的可用性
    /// </summary>
    protected void ShowButtonPaging()
    {
        //
        //默认情况都为可显示.
        //
        linkbtnFirst.Enabled = true;
        linkbtnPre.Enabled = true;
        linkbtnNext.Enabled = true;
        linkbtnLast.Enabled = true;

        if (lblPage.Text == "1")
        {
            //
            //如果是第一页，则将首页与上一页的按钮设为不可用
            //
            linkbtnFirst.Enabled = false;
            linkbtnPre.Enabled = false;
            linkbtnLast.Enabled = false;
            linkbtnNext.Enabled = false;
        }//if (lblPage.Text == "1")
        else if (int.Parse(lblPage.Text) == iPageCount)
        {
            //
            //如果是最后一页，则将末页与下一页设为不可用
            //
            linkbtnNext.Enabled = false;
            linkbtnLast.Enabled = false;
        }//else if (int.Parse(lblPage.Text) == iPageCount)

        else
        {
            //
            //其他情况全部为可用
            //
            linkbtnFirst.Enabled = true;
            linkbtnPre.Enabled = true;
            linkbtnNext.Enabled = true;
            linkbtnLast.Enabled = true;

        }//else
    }
    /// <summary>
    /// 选择页的索引
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(ddlPage.SelectedValue) + 1);
        BindDataList(int.Parse(lblPage.Text));
    }

    /// <summary>
    /// 首页
    /// </summary>
    /// <param name="sender">对象</param>
    /// <param name="e">参数</param>
    protected void linkbtnFirst_Click(object sender, EventArgs e)
    {
        BindDataList(1);
    }
    /// <summary>
    /// 下一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void linkbtnNext_Click(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(lblPage.Text) + 1);
        BindDataList(int.Parse(lblPage.Text));
    }


    /// <summary>
    /// 上一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void linkbtnPre_Click(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(lblPage.Text) - 1);
        BindDataList(int.Parse(lblPage.Text));
    }


    /// <summary>
    /// 末页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void linkbtnLast_Click(object sender, EventArgs e)
    {
        BindDataList(iPageCount);
    }
    /// <summary>
    /// dlstProduct对数据的绑定.
    /// </summary>
    /// <param name="sender">对象</param>
    /// <param name="e">参数</param>
    protected void dlstProduct_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        string strProductID = ((Label)e.Item.FindControl("lblProductID")).Text;

        ((ImageButton)e.Item.FindControl("imgBtnProducter")).ImageUrl = "../ImagInfo.aspx?productID=" + strProductID;

        ((ImageButton)e.Item.FindControl("imgBtnProducter")).PostBackUrl = "../ProductInfo.aspx?productID=" + strProductID;
    }
    /// <summary>
    /// 对商品进行排序(价格,名称,时间)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dropSort_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataList(int.Parse(lblPage.Text));
    }
}
